import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { config } from '../../shared/smartadmin.config';
import { Router } from "@angular/router"; //路由引入

declare var $: any;
@Component({
	selector: 'sa-stopMark',
	templateUrl: './stopMark.component.html',
})
export class StopMarkComponent implements OnInit {
	
	tableData: any[] = []; //table数据
	totalCount: number = 0; //分页总条数
	curPage: number = 1; //分页当前页
	pageSize: number = 10; //内置的分页每页条数
	searchKey: string = ''; //搜索keyword
	tableHeardlist=[
		{keys:'carNumber',value:'车牌号码 '},
		{keys:'weight',value:'单桶重量'},
		{keys:'uploadDate',value:'时间'},
		{keys:'address',value:'地址'},
	];
	tableQueryitem={
		startTime:'', 	//开始时间
		endTime:  '', 	//结束时间
		companyId: '',
		companyName: '',
		deviceId: '', 	//设备ID
		carNumber:  '',
		searchKey:'', 	//搜索keyword
	}
	companyData = {
		"address": "",
		"carNumber": "",
		"companyId": "",
		"companyName": "",
		"createDate": "",
		"deviceId": "",
		"endDate": "",
		"id": "",
		"lastUpdateDate": "",
		"lat": "",
		"lon": "",
		"message": "",
		"second": "",
		"startDate": "",
		"updateDate": "",
		"status":","
	}
	constructor(private jsonApiService: JsonApiService,private router: Router) {}
	getSelectTableTime(time){
		this.tableQueryitem.startTime=time.startTime;
		this.tableQueryitem.endTime=time.endTime;
	}
	
	companySelected(val){
		this.tableQueryitem.deviceId=val.deviceId;
		this.tableQueryitem.carNumber=val.carNumber;
		this.tableQueryitem.companyId=val.companyId;
		this.tableQueryitem.companyName=val.companyName;
	}
	ngOnInit() {
		this.load();
		this.initDate();
		
	}
	
	//导出报表
	export_totle() {
		if(this.tableQueryitem.startTime == '' || this.tableQueryitem.endTime == '') {
			this.jsonApiService.message("起始结束时间不能为空");
			return;
		}
		let timeCheck = common.correctTimeParams(this.tableQueryitem.startTime, this.tableQueryitem.endTime, 0);
		if(!timeCheck.status) {
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		var parameters = {
			deviceId: this.tableQueryitem.deviceId,
			startTime: this.tableQueryitem.startTime,
			endTime: this.tableQueryitem.endTime,
		}
		common.exprotDataFile(parameters, 'param', config.BaseURL + 'weightStatistics/exportWeightDataStatistics.html')
	}
	//时间转化
	getTime(date) {
		var year = date.getFullYear();
		var month = (date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1);
		var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();

		return year + "-" + month + "-" + day;
	}
	//初始化加载list数据
	load() {
		let timeCheck=common.correctTimeParams(this.tableQueryitem.startTime,this.tableQueryitem.endTime,0);
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				deviceId:this.tableQueryitem.deviceId,
				companyId:this.tableQueryitem.companyId,
				startTime:this.tableQueryitem.startTime,
				endTime:this.tableQueryitem.endTime,
				searchKey:this.tableQueryitem.searchKey,
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http("parkDotManage/parkDotforpage.html", reqdata).subscribe(result => {
			if(!result.isSuccess){
				this.jsonApiService.message(result.message);
				return;
			}
//			for(let item of result.exhibitDatas){
//				item.weight=common.twoDecimal(item.weight);
//			}
			
			this.tableData = result.exhibitDatas;
			this.tableData.map(item=>{
				item.second = common.secondToDate(item.second);
			});
			this.totalCount = result.recordCount;
		})
	}
	//刷新
    refresh(){
    	this.load();
    }
	//查询
	table_search(){
		this.curPage=1;
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.load();
	}
	ngOnDestroy() {
		document.onclick = null;
	}

	// 删除
	deleteRow(row){
		this.jsonApiService.confirm_message('此操作将永久删除数据,确认要删除').then(()=>{
			let reqdata = 'id=' + row.id;
			this.jsonApiService.http("parkDotManage/deleteParkDot.html",reqdata).subscribe(data => {
				this.jsonApiService.message(data.message)
				if(data.success){
					this.load();
				}
			})
		})
	}
	// 编辑
	editRow(id,val){
		val.show();
		let reqdata = 'id=' + id;
		this.jsonApiService.http("parkDotManage/queryParkDotById.html", reqdata).subscribe(result => {
			this.companyData = result.data;
		});
	}
	// 取消
	cancelPosition(val){
		val.hide();
	}
	// 保存
	save(val){
		// 拼接数据传值
		this.companyData.endDate = $('#endDate').val();
		this.companyData.startDate = $('#startDate').val();
		// let reqdata =`address=${this.companyData.address}&carNumber=${this.companyData.carNumber}&companyId=${this.companyData.companyId}&companyName=${this.companyData.companyName}&createDate=${this.companyData.createDate}&deviceId=${this.companyData.deviceId}&endDate=${this.companyData.endDate}&id=${this.companyData.id}&lastUpdateDate=${this.companyData.lastUpdateDate}&lat=${this.companyData.lat}&lon=${this.companyData.lon}&message=${this.companyData.message}&second=${this.companyData.second}&startDate=${this.companyData.startDate}&status=${this.companyData.status}&updateDate=${this.companyData.updateDate}`;
		let reqdata =`id=${this.companyData.id}&deviceId=${this.companyData.deviceId}&carNumber=${this.companyData.carNumber}&startDate=${this.companyData.startDate}&endDate=${this.companyData.endDate}`;
		this.jsonApiService.http("parkDotManage/updateParkDot.html", reqdata).subscribe(result => {
			this.jsonApiService.message(result.message)
			if(result.success){
				val.hide();
				this.load();
			}			
		});
	}
	
	//初始化日期
	initDate(){
		var that = this;
		return new Promise(function (resolve, reject) {
			System.import('assets/js/datepicker/js/bootstrap-datetimepicker.min.js').then(()=>{
					System.import('assets/js/datepicker/locales/bootstrap-datetimepicker.zh-CN.js').then(()=>{
						$("#startDate").datetimepicker({//时间插件
							format: "yyyy-mm-dd hh:ii:ss",
						    language: "zh-CN",
						    autoclose: true,
						    // minView : 2
						}).on("changeDate", function(e){
							var date = e.date;
							if(!date){return;}
							var time = that.getTime(date);
							$("#endDate").datetimepicker("setStartDate", time);
						});
						$("#endDate").datetimepicker({//时间插件
							format: "yyyy-mm-dd hh:ii:ss",
						    language: "zh-CN",
						    autoclose: true,
						    // minView : 2
						}).on("changeDate", function(e){
							var date = e.date;
							if(!date){return;}
							var time = that.getTime(date);
							$("#startDate").datetimepicker("setEndDate", time);
						});
						// var date = new Date();
						// $("#datetimepicker2").val(that.getTime(date));//当前时间
						// date.setDate(date.getDate() - 7);//设置前一周
						// $("#datetimepicker1").val(that.getTime(date));
						// resolve();
					});
				}
			);
		})
	}
	
}